/**
 * 版权所有 Copyright@@2016-03-18 1:40:18
 */
package com.fairy.sso.service.impl;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fairy.sso.Ticket;
import com.fairy.sso.biz.PerUserBiz;
import com.fairy.sso.biz.TicketBiz;
import com.fairy.sso.entity.UserEntity;
import com.fairy.sso.entity.SSOUserEntity;
import com.fairy.sso.entity.TicketEntity;
import com.fairy.sso.service.SSOService;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * [UserServiceImpl.java.java]
 *
 * @Author: [yyk1504@163.com]
 * @CreateDate: [2016-03-18 1:40:18]
 * @Update: [说明本次修改内容] by [yyk1504@163.com] [2016-03-18 1:40:18]
 * @Version: [v1.0]
 */
@Service("ssoService")
public class SSOServiceImpl implements SSOService<SSOUserEntity>{

	Logger log = Logger.getLogger(SSOServiceImpl.class);
	@Autowired
	private PerUserBiz perUserBiz;
	
	@Autowired
	private TicketBiz ticketBiz;
	
	@Override
	public SSOUserEntity login(String accountNo, String passWord) {
		//通过接口实现用户验证
		SSOUserEntity ssoUserEntity=new SSOUserEntity();
		String em = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
		//String ph = " ^[1][358][0-9]{9}$";
		String ph = "^[1][3578]\\d{9}$";
		UserEntity entity=new UserEntity();
		if(accountNo.matches(em))
		{    //邮箱登录
			entity.setEmail(accountNo);
			log.info("邮箱登录:"+accountNo);
		} 
		else if(accountNo.matches(ph))
		{
			//手机号登录
			entity.setMobile(accountNo);
			log.info("手机号登录:"+accountNo);
		}
		else
		{
			//就是用户名登录
			//user.setUsername(username);
			entity.setUserName(accountNo);
			log.info("用户名登录:"+accountNo);
		}
		entity.setUserPass(passWord);
		entity=perUserBiz.login(entity);
		ssoUserEntity.setAccount(accountNo);
		ssoUserEntity.setUserId(entity.getUserId());
		ssoUserEntity.setUserName(entity.getUserName());
		return ssoUserEntity;
	}

	@Override
	public SSOUserEntity loginByMobilePass(String mobileNo, String passWord) {
		//通过接口实现用户验证
		SSOUserEntity ssoUserEntity=new SSOUserEntity();
		ssoUserEntity.setAccount(mobileNo);
		ssoUserEntity.setUserId("test_0101_kljhh_uu");
		ssoUserEntity.setUserName(mobileNo);
		return ssoUserEntity;
	}

	@Override
	public boolean saveTicket(Ticket ticket,String key) {
		boolean bl=false;
		TicketEntity ticketEntity=new TicketEntity();
		ticketEntity.setEnable(0);
		ticketEntity.setKey(key);
		ticketEntity.setCreateTime(ticket.getTime()+"");
		ticketEntity.setTicket(ticket.toString());
		
		ticketBiz.save(ticketEntity);
		bl=true;
		return bl;
	}

	@Override
	public Ticket loadTicket(String key) {
		TicketEntity ticketEntity=ticketBiz.getById(key);
		Ticket ticket=new Ticket();
		if(null!=ticketEntity)
		{
			String st=ticketEntity.getTicket();
			ObjectMapper mapper = new ObjectMapper(); 
			try {
				ticket = mapper.readValue(st, Ticket.class);
				log.info("Ticket is::"+ticket.toString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return ticket;
	}
	@Override
	public boolean deleteTicketByKey(String key) {
		boolean bl=false;

		
		ticketBiz.deleteById(key);;
		bl=true;
		return bl;
	}

}
